﻿using DoNet.ViewFaceCore.Sdk;
using System;
using System.Diagnostics;
using System.Text;

namespace DoNet.ViewFaceCore.Framework
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = Stopwatch.StartNew();
            string imageUrl = "test.jpg";
            //string imageUrl = "test_01.jpeg";
            // 加载图片
            using (var bitmap = FaceDiscern.GetSKBitmap(imageUrl))
            {
                // 正确的转换方式：使用ToFaceImage()方法
                using (var faceImage = FaceDiscern.GetFaceImage(bitmap))
                {
                    // 检测人脸
                    var faces = FaceDiscern.GetFaceInfos(faceImage);
                    Console.WriteLine($"检测到 {faces.Length} 张人脸");

                    int i = 0;
                    foreach (var face in faces)
                    {
                        i++;
                        sw.Restart();

                        StringBuilder sb = new StringBuilder();
                        sb.Append($"{i}\t人脸置信度：{face.Score}\t人脸位置：{face.Location}");
                        //人脸信息 info 的关键点坐标
                        var points = FaceDiscern.GetFaceMarkPoint(faceImage, face);
                        sb.Append($"\t标记关键点{points.Length}个");
                        // 预测年龄
                        int age = FaceDiscern.GetPredictAge(faceImage, points);
                        sb.Append($"\t预测年龄：{age}");
                        // 预测性别
                        var gender = FaceDiscern.GetPredictGender(faceImage, points);
                        sb.Append($"\t预测性别：{EnumHelper.GetEnumDescription(gender)}");
                        // 眼睛状态检测
                        var eyeState = FaceDiscern.GetEyeState(faceImage, points);
                        sb.Append($"\t眼睛状态：左眼状态({EnumHelper.GetEnumDescription(eyeState.LeftEyeState)})，右眼状态({EnumHelper.GetEnumDescription(eyeState.RightEyeState)})");

                        // 戴口罩人脸识别
                        var plotMask = FaceDiscern.GetPlotMask(faceImage, face);
                        sb.Append($"\t是否戴口罩：{(plotMask.Masked ? "是" : "否")}，评估分数：{plotMask.Score}");
                        sb.Append($"\t质量检测：");
                        var brightness = FaceDiscern.GetQualityType("Brightness");
                        var brightnessResult = FaceDiscern.GetFaceQuality(bitmap, face, points, brightness);
                        sb.Append($"{EnumHelper.GetEnumDescription(brightness)}(等级：{EnumHelper.GetEnumDescription(brightnessResult.Level)}，分数：{brightnessResult.Score})；");
                        var resolution = FaceDiscern.GetQualityType("Resolution");
                        var resolutionResult = FaceDiscern.GetFaceQuality(bitmap, face, points, resolution);
                        sb.Append($"{EnumHelper.GetEnumDescription(resolution)}（等级：{EnumHelper.GetEnumDescription(resolutionResult.Level)}，分数：{resolutionResult.Score})；");
                        var clarity = FaceDiscern.GetQualityType("Clarity");
                        var clarityResult = FaceDiscern.GetFaceQuality(bitmap, face, points, clarity);
                        sb.Append($"{EnumHelper.GetEnumDescription(clarity)}（等级：{EnumHelper.GetEnumDescription(clarityResult.Level)}，分数：{clarityResult.Score})；");
                        var clarityEx = FaceDiscern.GetQualityType("ClarityEx");
                        var clarityExResult = FaceDiscern.GetFaceQuality(bitmap, face, points, clarityEx);
                        sb.Append($"{EnumHelper.GetEnumDescription(clarityEx)}（等级：{EnumHelper.GetEnumDescription(clarityExResult.Level)}，分数：{clarityExResult.Score})；");
                        var integrity = FaceDiscern.GetQualityType("Integrity");
                        var integrityExResult = FaceDiscern.GetFaceQuality(bitmap, face, points, integrity);
                        sb.Append($"{EnumHelper.GetEnumDescription(integrity)}（等级：{EnumHelper.GetEnumDescription(integrityExResult.Level)}，分数：{integrityExResult.Score})；");
                        var structure = FaceDiscern.GetQualityType("Structure");
                        var structureeResult = FaceDiscern.GetFaceQuality(bitmap, face, points, structure);
                        sb.Append($"{EnumHelper.GetEnumDescription(structure)}（等级：{EnumHelper.GetEnumDescription(structureeResult.Level)}，分数：{structureeResult.Score})；");
                        var pose = FaceDiscern.GetQualityType("Pose");
                        var poseResult = FaceDiscern.GetFaceQuality(bitmap, face, points, pose);
                        sb.Append($"{EnumHelper.GetEnumDescription(clarity)}（等级：{EnumHelper.GetEnumDescription(poseResult.Level)}，分数：{poseResult.Score})；");
                        var poseEx = FaceDiscern.GetQualityType("PoseEx");
                        var poseExeResult = FaceDiscern.GetFaceQuality(bitmap, face, points, poseEx);
                        sb.Append($"{EnumHelper.GetEnumDescription(poseEx)}（等级：{EnumHelper.GetEnumDescription(poseExeResult.Level)}，分数：{poseExeResult.Score})");

                        var antiSpoofing = FaceDiscern.GetAntiSpoofing(faceImage, face, points);
                        sb.Append($"\t活体检测：{EnumHelper.GetEnumDescription(antiSpoofing.Status)}，清晰度: {antiSpoofing.Clarity}，真实度：{antiSpoofing.Reality}");

                        Console.WriteLine(sb.ToString() + $"\t耗时：{sw.ElapsedMilliseconds}ms");
                    }
                }
            }
            sw.Stop();
        }
    }
}
